import Base from "../model/base/Base.js";

export class qf extends Base {
  constructor() {
    super({
      name: "ad[私发/群发]",
      dsc: "",
      event: "message",
      priority: -1,
      rule: [
        {
          reg: "^#一键群发",
          fnc: "qf",
        },
        {
          reg: "^#一键私发",
          fnc: "hyqf",
        },
        {
          reg: "^#?一键打卡$",
          fnc: "dk",
        },
        {
          reg: "^#?(开启|关闭)(一键)?(群)?打卡$",
          fnc: "kqdk",
        },
      ],
    });
    this.task = [
      {
        cron: "5 0 0 * * ?",
        name: "[ad插件]一键打卡",
        fnc: () => this.dk(this.e, true),
      },
    ];
  }
  async kqdk(e) {
    // this.cm.tj().catch()
    let msg = e.msg.match(/^#?(开启|关闭)(一键)?(群)?打卡$/);
    msg = msg[1] ? msg[1] : false;
    if (msg === "开启") {
      await redis.set("ad:daka", "true");
      e.reply("成功");
    } else {
      await redis.set("ad:daka", "false");
      e.reply("成功");
    }
  }
  async dk(e, isauto = false) {
    // this.cm.tj().catch()
    if (!isauto) {
      if (e.isMaster || (await this.cm.check(e.user_id))) {
        for (let group of Bot[e.self_id].gl.keys()) {
          Bot.pickGroup(group).sign();
        }
        await e.reply("打卡完成");
      }
    } else {
      let uins = Bot.uin.toString();
      uins = uins.split(",");

      let daka = await this.GetredisKey("ad:daka", "false");
      if (daka === "true") {
        for (let uin of uins) {
          // 检查uin中是否包含英文字符
          if (/^[a-zA-Z]+$/.test(uin)) {
            continue;
          }
          for (let group of Bot[uin].gl.keys()) {
            Bot.pickGroup(group).sign();
          }
        }
      } else {
        return;
      }
    }
  }
  async qf(e) {
    let msgs = e.message[0].text.split(" ");
    if (e.isMaster || (await this.cm.check(e.user_id))) {
      // this.cm.tj().catch()
      e.message[0].text = e.message[0].text.replace(/.*一键群发/, "").trim();
      if (!e.message[0].text) e.message.shift();
      if (e.message.length === 0) return e.reply("❎ 消息不能为空");
      for (let group of Bot[e.self_id].gl.keys()) {
        Bot[e.self_id].pickGroup(group).sendMsg(e.message);
        await e.runtime.common.sleep(2500);
      }
    } else {
      await e.reply("你没有权限");
      return;
    }
  }
  async hyqf(e) {
    let msgs = e.message[0].text.split(" ");
    if (e.isMaster || (await this.cm.check(e.user_id))) {
      // this.cm.tj().catch()
      e.message[0].text = e.message[0].text.replace(/.*一键私发/, "").trim();
      if (!e.message[0].text) e.message.shift();
      if (e.message.length === 0) return e.reply("❎ 消息不能为空");
      for (let friend of Bot[e.self_id].fl.keys()) {
        Bot[e.self_id].pickFriend(friend).sendMsg(e.message);
        await e.runtime.common.sleep(2500);
      }
    } else {
      await e.reply("你没有权限");
      return;
    }
  }
  async PICKGR() {
    let msgtosendGR = e.msg;
    let msgtosend = this.e.msg;
    if (!msgtosendGR || this.e.msg === "取消") {
      await this.e.reply("已结束发送");
      this.finish("PICKGR");
    }
    for (let group of Bot.gl.keys()) {
      Bot.pickGroup(group).sendMsg(msgtosendGR);
      await this.e.runtime.common.sleep(500);
    }
  }
  async PICKFR() {
    let msgtosend = this.e.msg;
    if (!msgtosend || this.e.msg === "取消") {
      await this.e.reply("已结束发送");
      this.finish("PICKFR");
    }
    for (let group of Bot.fl.keys()) {
      Bot.pickFriend(group).sendMsg(msgtosend);
      await this.e.runtime.common.sleep(500);
    }
  }
  async GetredisKey(key, defaultValue) {
    const value = await redis.get(key);
    if (value === null) {
      await redis.set(key, defaultValue);
      return defaultValue;
    } else {
      return value;
    }
  }
}
